//
//  BADTabBar.m
//  BAD
//
//  Created by Can on 2018/5/14.
//  Copyright © 2018年 hwd. All rights reserved.
//

#import "BADTabBar.h"
#import <objc/runtime.h>
#import "UIView+LBExtension.h"
#define LBMagin 10

@interface BADTabBar()

@property (nonatomic, weak) UIButton *button ;

@end

@implementation BADTabBar

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self=[super initWithFrame:frame]) {

        self.backgroundColor = [UIColor whiteColor];

        UIButton *plusBtn = [[UIButton alloc] init];
        [plusBtn setBackgroundImage:[UIImage imageNamed:@"logo2"] forState:UIControlStateNormal];
        [plusBtn setBackgroundImage:[UIImage imageNamed:@"logo2"] forState:UIControlStateHighlighted];
        
        self.button = plusBtn;
        
        [self addSubview:plusBtn];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    //系统自带的按钮类型是UITabBarButton，找出这些类型的按钮，然后重新排布位置，空出中间的位置
    Class class = NSClassFromString(@"UITabBarButton");
    
//    self.button.centerX = self.centerX;
//    //调整发布按钮的中线点Y值
//    self.button.centerY = 49/2 - 2*LBMagin;
    
    self.button.frame = CGRectMake((SCREENW - self.button.currentBackgroundImage.size.width)/2, -self.button.currentBackgroundImage.size.height/2, self.button.currentBackgroundImage.size.width, self.button.currentBackgroundImage.size.height);
    
//    self.button.size = CGSizeMake(self.button.currentBackgroundImage.size.width, self.button.currentBackgroundImage.size.height);

    int btnIndex = 0;
    for (UIView *btn in self.subviews) {//遍历tabbar的子控件
        if ([btn isKindOfClass:class]) {//如果是系统的UITabBarButton，那么就调整子控件位置，空出中间位置
            //每一个按钮的宽度==tabbar的3分之一
            btn.width = self.width / 3;
            
            btn.x = btn.width * btnIndex;
            
            btnIndex++;
            //如果是索引是1(从0开始的)，直接让索引++，目的就是让消息按钮的位置向右移动，空出来发布按钮的位置
            if (btnIndex == 1) {
                btnIndex++;
            }
            
        }
    }
    
    [self bringSubviewToFront:self.button];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
